"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:FinanceFGckCtrl
 * @description
 * # FinanceFGckCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller(
    "FinanceFGckCtrl",
    function($scope, $rootScope, openApi, Util, $location, $routeParams, localStorageService, FormSubmit, TaskReviewService) {
      var id = ($scope.process_id = $routeParams.id);
      // 底部款项类型选框添加
      $scope.deductTypes = [
        {
          key: "收入",
          value: 0
        },
        {
          key: "暂扣",
          value: 3
        },
        {
          key: "缴纳",
          value: 2
        },
        {
          key: "押金",
          value: 1
        }
      ];
      $scope.hasInvoiceArr = [
        {
          key: "已提供",
          value: "已提供"
        },
        {
          key: "未提供",
          value: "未提供"
        }
      ];
      // 是否提供发票
      $scope.invoicechange = function() {
        if ($scope.param.hasInvoice == "未提供") {
          $scope.param.taxReceiptNo = "";
          $scope.param.taxReceiptNo2 = "";
          $scope.param.taxReceiptNo3 = "";
        }
      };
      // 大类更改时候小类清空
      $scope.payTypechange = function() {
        $scope.param.payType2 = "";
        $scope.getgckTypeObjA();
      };
      $scope.showgckTypeObj = function() {
        var param = {};
        param.type = "工程款类型";
        Util.getKeyValue(param, function(data) {
          $scope.gckTypeList = data.data;
        });
      };
      $scope.getgckTypeObjA = function() {
        var param = {};
        param.type = "工程款类型";
        param.parent = $scope.param.payType1;
        Util.getKeyValue(param, function(data) {
          $scope.gckTypeListA = data.data;
        });
      };
      $scope.showSearchPicker=function(){
        var param={}
        param.type=2;
        $scope.searchparam=param
        $scope.searchtitle="收款人"
        $scope.apiurl=openApi.projectMoneyHistory
        $scope.showSearchInput=true;
        $rootScope.$emit('showmasker');
    }
    $scope.handleSearch=function(data){
        $scope.param.acceptCompany=data[0] 
        $scope.param.acceptAccount=data[1]
        $scope.param.acceptBank=data[2]
        hideSearchPicker()
    }
    var hideSearchPicker = function () {
        $rootScope.$emit('closemasker');
        $scope.showSearchInput = false;
    };
    $scope.$on('closemasker', function () {
        hideSearchPicker()
    });
      $scope.choosePeople = function() {
        var relModuleObj = {
          name: "FinanceFGckCtrl",
          selectTab: "project_money_select",
          selectTabContent: "工程名称"
        };
        // 清掉关联的编号
        $scope.param.taxReceiptNo = "";
        Util.savaParamForReview($scope.param, id);
        Util.savaDataforsearch(relModuleObj);
        $location.path("finance/choosepeople");
      };
      $scope.choosegck = function() {
        var relModuleObj = {
          name: "FinanceFGckCtrl",
          selectTab: "receive_money",
          selectTabContent: "工程款"
        };
        // 清掉关联的编号
        Util.savaParamForReview($scope.param, id);
        Util.savaDataforsearch(relModuleObj);
        $location.path("finance/choosepeople");
      };
      $scope.chooseRecord = function() {
        if (!$scope.param.projectName) {
          Util.Toast("请先选择工程名称");
          return;
        }
        var relModuleObj = {
          selectTab: "select_tax_record",
          param: {
            contractNo: $scope.param.contractNo
          },
          propertyName: "taxRecordNo",
          propertyValue: "fileNo",
          selectTabContent: "税票编号"
        };
        // 清掉关联的编号
        var params = $scope.param;
        Util.savaParamForReview(params, id);
        Util.savaDataforsearch(relModuleObj);
        $location.path("finance/choosemultinfo");
      };

      $scope.chooseContract = function() {
        var relModuleObj = {
          selectTab: "select_common_contract",
          selectTabContent: "通用合同名称"
        };
        if ($scope.showCerPicker) {
          localStorageService.set("pickerdata", $scope.picker);
          hidePayment();
        }
        Util.savaParamForReview($scope.param, id);
        Util.savaDataforsearch(relModuleObj);
        $location.path("finance/choosepeople");
      };
      $scope.chooseTax = function(type) {
        if ($scope.param.hasInvoice !== "已提供") {
          Util.Toast("请选择缴税发票");
          return;
        }
        if (!$scope.param.projectName) {
          Util.Toast("请选择工程名称");
          return;
        }
        var relModuleObj = {
          name: "FinanceFGckCtrl",
          selectTab: "project_money_select_tax",
          propertyName: type,
          param: {
            filterSql: $scope.param.contractNo
          },
          selectTabContent: "缴税发票编号"
        };
        Util.savaParamForReview($scope.param, id);
        Util.savaDataforsearch(relModuleObj);
        $location.path("finance/choosepeople");
      };
      $scope.choosePayment = function() {
        if (!$scope.param.projectName) {
          Util.Toast("请先选择工程名称");
          return;
        }
        var relModuleObj = {
          selectTab: "select_payment_invoice",
          propertyName: "paymentInvoiceNo",
          propertyValue: "fileNo",
          param: {
            contractNo: $scope.param.contractNo
          },
          selectTabContent: "发票编号"
        };
        if ($scope.showCerPicker) {
          localStorageService.set("pickerdata", $scope.picker);
          hidePayment();
        }
        // 清掉关联的编号
        var params = $scope.param;
        Util.savaParamForReview(params, id);
        Util.savaDataforsearch(relModuleObj);
        $location.path("finance/choosemultinfo");
      };

      // -----------------------付款明细---------
      $scope.showPayment = function() {
        $rootScope.$emit("showmasker");
        $scope.showCerPicker = true;
      };
      $scope.pickerPayTypechange = function() {
        $scope.picker.payType2 = "";
        $scope.getgckTypeObjB();
      };
      $scope.getgckTypeObjB = function() {
        var param = {};
        param.type = "工程款类型";
        param.parent = $scope.picker.payType1;
        Util.getKeyValue(param, function(data) {
          $scope.gckTypeListB = data.data;
        });
      };
      
      $scope.deletedItem = function(index) {
        $scope.param.paymentList.splice(index, 1);
        $scope.index = -1;
      };
      $scope.addPayment = function() {
        $scope.picker = $scope.picker || {};
        var check = false;
        check = checkPicker($scope.picker);
        if (!check) {
          return;
        }
        $scope.param.paymentList.push($scope.picker);
        // 隐藏表单
        hidePayment();
      };
      var hidePayment = function() {
        $scope.picker = {};
        $rootScope.$emit("closemasker");
        $scope.showCerPicker = false;
      };
      $scope.$on("closemasker", function() {
        hidePayment();
      });
      $scope.cacelPayment = function() {
        hidePayment();
      };
      $scope.swipe = function(index) {
        $scope.paymentIndex = index;
      };
      $scope.resetlist = function(index) {
        $scope.paymentIndex = -1;
      };
      // 验证是否输入完整
      var checkPicker = function(data) {
        var check = true;
        if (!data.paymentAmount) {
          Util.Toast("请填写付款金额");
          check = false;
          return check;
        }
        if (!data.acceptCompany) {
          Util.Toast("请填写收款人");
          check = false;
          return check;
        }
        if (!data.acceptAccount) {
          Util.Toast("请填写收款人账号");
          check = false;
          return check;
        }
        if (!data.acceptBank) {
          Util.Toast("请填写收款人开户行");
          check = false;
          return check;
        }
        if (!data.payType1) {
          Util.Toast("请选择工程款大类");
          check = false;
          return check;
        }
        if (!data.payType2) {
          Util.Toast("请选项工程款小类");
          check = false;
          return check;
        }
        return check;
      };
      // ----------------付款明细end------------
      // -----------------------扣款明细---------
      $scope.showDeducts = function() {
        $rootScope.$emit("showmasker");
        $scope.showCerPicker2 = true;
      };
      $scope.deletedDeduct = function(index) {
        $scope.param.deductList.splice(index, 1);
        $scope.deductListIndex = -1;
      };
      $scope.addDeducts = function(data) {
        var check = false;
        check = checkDeducts(data);
        if (!check) {
          return;
        }
        $scope.param.deductList.push(data);
        // 隐藏表单
        hideDeducts();
      };
      var hideDeducts = function() {
        $scope.picker2 = {};
        $rootScope.$emit("closemasker");
        $scope.showCerPicker2 = false;
      };
      $scope.$on("closemasker", function() {
        hideDeducts();
      });
      $scope.cacelDeducts = function() {
        hideDeducts();
      };
      $scope.swipededuct = function(index) {
        $scope.deductListIndex = index;
      };
      $scope.resetdeduct = function(index) {
        $scope.deductListIndex = -1;
      };
      // 验证是否输入完整
      var checkDeducts = function(data) {
        var check = true;
        if (!data.deductName) {
          Util.Toast("请填写扣款项目");
          check = false;
          return check;
        }
        if (typeof data.deductType=="undefined") {
          Util.Toast("请选择扣款类型");
          check = false;
          return check;
        }
        if (!data.deductAmount) {
          Util.Toast("请填写扣款金额");
          check = false;
          return check;
        }
        return check;
      };
      // ----------------扣款明细end------------
      $scope.submit = function(operationId) {
        $scope.param.payments = angular.toJson($scope.param.paymentList);
        $scope.param.deducts = angular.toJson($scope.param.deductList);
        // 底部类型end
        var options = {};
        // 流程id
        options.id = id;
        // 根据operationId判断是草稿还是提交
        options.operationId = operationId;
        options.urlParam = openApi.projectMoney;
        options.param = angular.copy($scope.param, {});
        delete options.param.paymentList;
        delete options.param.deductList;
        FormSubmit.submit(options,$scope);
      };
      $scope.validate = function() {
        if (!$scope.param.projectName) {
          Util.Toast("请填写工程名称");
          return;
        }
        if (!$scope.param.payType1) {
          Util.Toast("请填写工程款类型(大类)");
          return;
        }
        if (!$scope.param.payType2) {
          Util.Toast("请填写工程款类型(小类)");
          return;
        }
        if ($scope.param.projectName && $scope.param.hasInvoice == "已提供") {
          if (
            !($scope.param.taxReceiptNo ||
              $scope.param.taxReceiptNo2 ||
              $scope.param.taxReceiptNo3)
          ) {
            Util.Toast("请选择缴税发票编号");
            return;
          }
        }
        if (!$scope.param.amount) {
          if ($scope.param.amount !== 0) {
            Util.Toast("请填写付款金额");
            return;
          }
        }
        if (!$scope.param.acceptCompany) {
          Util.Toast("请填写收款人");
          return;
        }
        if (!$scope.param.acceptAccount) {
          Util.Toast("请填写收款人账号");
          return;
        }
        if (!$scope.param.acceptBank) {
          Util.Toast("请填写收款人开户行");
          return;
        }
        if (!$scope.validation.passvalidation) {
          Util.Toast($scope.validation.tip);
          return;
        }
        return true;
      };

      // 初始化逻辑,如果是修改页面的话则先从接口请求数据回显，再将缓存中的跳转来源删除，方便页面跳转返回之后的回显
      var init = function() {
        // 获取回显的详细信息
        Util.getDetailData(id, function(data) {
          $scope.param = data;
          $scope.param.paymentList = $scope.param.paymentList || [];
          $scope.param.deductList = $scope.param.deductList || [];
          $scope.getgckTypeObjA();
          var pickerdata = localStorageService.get("pickerdata");
          if (pickerdata) {
            localStorageService.remove("pickerdata");
            $scope.picker =pickerdata;
            if (pickerdata.payType1) {
              $scope.getgckTypeObjB();
            }
            $scope.showPayment();
          }
        });
        $scope.showgckTypeObj();
      };
      // 初始化
      init();
    }
  );
